#!/usr/bin/env python


import numpy as np
import scipy as sp
import scipy.interpolate as spin
import matplotlib.pyplot as plt

degree = 5

# generate a noisy parabola
t = np.linspace(0,100,200)
parabola = t**2
noise = np.random.normal(0,300,200)
y = parabola + noise 

# form the Vandermonde matrix
#A = np.vander(t, degree)
# find the x that minimizes the norm of Ax-y
#(coeffs, residuals, rank, sing_vals) = np.linalg.lstsq(A, y) 
# create a polynomial using coefficients
#f = np.poly1d(coeffs)

#f = spin.interp1d(t, y, kind='linear')
f = spin.interp1d(t, y, kind='nearest')

# for plot, estimate y for each observation time
y_est = f(t)
 
# create plot
plt.plot(t, y, '.', label = 'original data', markersize=5)
plt.plot(t, y_est, 'o-', label = 'estimate', markersize=1)
plt.xlabel('time')
plt.ylabel('sensor readings')
plt.title('least squares fit of degree 5')
#plt.savefig('sample.png')
plt.show()
plt.clf()

